package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.consts.EwcrmConstants;
import com.chhuang.ewcrm.entity.SysMenu;
import com.chhuang.ewcrm.entity.SysRole;
import com.chhuang.ewcrm.entity.SysRoleMenu;
import com.chhuang.ewcrm.entity.SysUser;
import com.chhuang.ewcrm.service.SysMenuService;
import com.chhuang.ewcrm.service.SysRoleMenuService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping
public class SiteMeshController {
    Logger logger= LoggerFactory.getLogger(SiteMeshController.class);

    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    @GetMapping
    public String login(){
        return "login";
    }

    @GetMapping("decorators/main")
    public ModelAndView main(HttpSession session){
        ModelAndView modelAndView = new ModelAndView("decorators/main");

        Object obju = session.getAttribute(EwcrmConstants.SessionConst.SYS_USER);
        if (obju != null) {
            SysUser user = (SysUser) obju;
            Object obj = session.getAttribute(EwcrmConstants.SessionConst.SYS_USER_MENUS+user.getUserId());
            if(obj==null) {

                SysMenu sysMenu = new SysMenu();
                sysMenu.setParentMenuId("fa-");
                List<SysMenu> menuList = this.sysMenuService.queryLike(sysMenu, "menu_order");//所有一级菜单

                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(user.getRoleId());
                List<SysRoleMenu> sysRoleMenus = sysRoleMenuService.query(sysRoleMenu, "menu_order");//角色权限菜单

                List<SysMenu> menus = new LinkedList<>();
                for (int i = 0; i < menuList.size(); i++) {
                    SysMenu item = menuList.get(i);
                    for (SysRoleMenu itemrm : sysRoleMenus) {
                        if (item.getMenuId().equals(itemrm.getMenuId())) {//角色有权限看此菜单
                            menus.add(item);
                        } else if (item.getMenuId().equals(itemrm.getParentMenuId())) {//角色有权限看此菜单下的子菜单
                            List<SysRoleMenu> temps = item.getSysRoleMenus();
                            if (temps == null) {
                                temps = new LinkedList<>();
                                temps.add(itemrm);
                                item.setSysRoleMenus(temps);
                                menus.add(item);
                            } else
                                temps.add(itemrm);
                        }
                    }
                }
                modelAndView.addObject("menus", menus);
                session.setAttribute(EwcrmConstants.SessionConst.SYS_USER_MENUS+user.getUserId(), menus);

            }else {
                modelAndView.addObject("menus", obj);
            }
        }

        return modelAndView;
    }

    /*@GetMapping("nullError")
    public String testError500(){
        String str = null;
        return str.toString();
    }*/
}
